﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using Rogan.Common;

namespace Rogan.DBUtility
{
    /// <summary>
    /// 数据访问抽象基础类
    /// Copyright (C) Rogan 
    /// </summary>
    public class DbHelperSQL : IDisposable
    {
        //数据库连接字符串(web.config来配置)，多数据库可使用DbHelperSQLP来实现.
        public string ConnectionString = PubConstant.ConnectionString;
        private IDbConnection _dbConnection;
        private IDbTransaction _dbTransaction;

        public bool IsInTransaction { get; set; }
        public string LastError { get; set; }

        #region 事务控制

        /// <summary>
        /// 方法BeginTran--开始一个事务
        /// </summary>
        /// <returns>
        ///		return --true 成功	--失败，请查看LastError
        /// </returns>
        public bool BeginTran()
        {
            return BeginTran(IsolationLevel.ReadCommitted);
        }

        /// <summary>
        /// 方法BeginTran--开始一个事务
        /// </summary>
        /// <param name="isolevel" type="IsolationLevel">
        /// 事务锁定行为
        /// </param>
        /// <returns>
        ///		return --true 成功	--失败，请查看LastError
        /// </returns>
        public bool BeginTran(IsolationLevel isolevel)
        {
            _dbConnection = new SqlConnection(ConnectionString);
            try
            {
                _dbConnection.Open();
            }
            catch (Exception e)
            {
                LogHelper.WriteError("BeginTran_Open", e);
                throw e;
            }
            if (_dbConnection.State == ConnectionState.Closed)//数据库未连接
            {
                LastError = "还没连接到数据库或连接已被关闭";
                return false;
            }
            if (this.IsInTransaction)//正在事务中
            {
                LastError = "已经在一个事务中";
                return false;
            }
            try
            {
                _dbTransaction = _dbConnection.BeginTransaction(isolevel);
                IsInTransaction = true;
                return true;
            }
            catch (Exception exp)
            {
                LogHelper.WriteError("BeginTran", exp);
                IsInTransaction = false;
                return false;
            }
        }

        /// <summary>
        /// 方法CommitTran--提交当前事务
        /// </summary>
        /// <returns>
        ///	return --true 成功   --false 失败
        /// </returns>
        public bool CommitTran()
        {
            if (_dbTransaction == null)//事务对象未初始化
            {
                LastError = "数据库事务未创建";
            }
            if (IsInTransaction)
            {
                try
                {
                    if (_dbTransaction != null) _dbTransaction.Commit();
                    IsInTransaction = false;
                    return true;
                }
                catch (Exception exp)
                {
                    LogHelper.WriteError("CommitTran", exp);
                    return false;
                }
                finally
                {
                    _dbConnection.Close();
                }

            }
            LastError = "数据库事务未开始";
            return true;
        }

        /// <summary>
        /// 方法CommitTran--回滚当前事务
        /// </summary>
        /// <returns>
        /// 	return --true 成功   --false 失败
        /// </returns>
        public bool RollBackTran()
        {
            if (this._dbTransaction == null)//事物对象未初始化
            {
                LastError = "数据库事务未创建";
                return true;
            }
            if (this.IsInTransaction)
            {
                try
                {
                    this._dbTransaction.Rollback();
                    this.IsInTransaction = false;
                    return true;
                }
                catch (Exception exp)
                {
                    LogHelper.WriteError("RollBackTran", exp);
                    return false;
                }
                finally
                {
                    _dbConnection.Close();
                }

            }
            else//不在事务中
            {
                LastError = "数据库事务未开始";
                return true;
            }

        }

        #endregion

        #region 公用方法
        ///// <summary>
        ///// 判断是否存在某表的某个字段
        ///// </summary>
        ///// <param name="tableName">表名称</param>
        ///// <param name="columnName">列名称</param>
        ///// <returns>是否存在</returns>
        //public bool ColumnExists(string tableName, string columnName)
        //{
        //    string sql = "select count(1) from syscolumns where [id]=object_id('" + tableName + "') and [name]='" + columnName + "'";
        //    object res = GetSingle(sql);
        //    if (res == null)
        //    {
        //        return false;
        //    }
        //    return Convert.ToInt32(res) > 0;
        //}
        public int GetMaxID(string FieldName, string TableName)
        {
            string strsql = "select max(" + FieldName + ")+1 from " + TableName;
            object obj = GetSingle(strsql);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return int.Parse(obj.ToString());
            }
        }
        public bool Exists(string strSql)
        {
            object obj = GetSingle(strSql);
            int cmdresult;
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString()); //也可能=0
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        ///// <summary>
        ///// 表是否存在
        ///// </summary>
        ///// <param name="TableName"></param>
        ///// <returns></returns>
        //public bool TabExists(string TableName)
        //{
        //    string strsql = "select count(*) from sysobjects where id = object_id(N'[" + TableName + "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1";
        //    //string strsql = "SELECT count(*) FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[" + TableName + "]') AND type in (N'U')";
        //    object obj = GetSingle(strsql);
        //    int cmdresult;
        //    if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
        //    {
        //        cmdresult = 0;
        //    }
        //    else
        //    {
        //        cmdresult = int.Parse(obj.ToString());
        //    }
        //    if (cmdresult == 0)
        //    {
        //        return false;
        //    }
        //    else
        //    {
        //        return true;
        //    }
        //}
        public bool Exists(string strSql, params SqlParameter[] cmdParms)
        {
            object obj = GetSingle(strSql, cmdParms);
            int cmdresult;
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion

        #region  执行简单SQL语句

        /// <summary>
        /// 执行SQL语句，返回影响的记录数
        /// </summary>
        /// <param name="sqlString">SQL语句</param>
        /// <returns>影响的记录数</returns>
        public int ExecuteSql(string sqlString)
        {
            SqlConnection conn = IsInTransaction && _dbConnection != null
                 ? _dbConnection as SqlConnection
                 : new SqlConnection(ConnectionString);


            using (SqlCommand cmd = new SqlCommand(sqlString, conn))
            {
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();
                    SqlTransaction tx = IsInTransaction ? this._dbTransaction as SqlTransaction : conn.BeginTransaction();
                    cmd.Transaction = tx;
                    int rows = cmd.ExecuteNonQuery();
                    if (!IsInTransaction) tx.Commit();
                    return rows;
                }
                catch (SqlException e)
                {
                    if (IsInTransaction == false) conn.Close();
                    throw e;
                }
            }

        }

        //public int ExecuteSqlByTime(string SQLString, int Times)
        //{
        //    SqlConnection conn = IsInTransaction && _dbConnection != null
        //       ? _dbConnection as SqlConnection
        //       : new SqlConnection(ConnectionString);

        //    using (SqlCommand cmd = new SqlCommand(SQLString, conn))
        //    {
        //        try
        //        {
        //            if (conn.State != ConnectionState.Open)
        //                conn.Open();
        //            SqlTransaction tx = IsInTransaction ? this._dbTransaction as SqlTransaction : conn.BeginTransaction();
        //            cmd.CommandTimeout = Times;
        //            cmd.Transaction = tx;
        //            int rows = cmd.ExecuteNonQuery();
        //            if (!IsInTransaction) tx.Commit();
        //            return rows;
        //        }
        //        catch (System.Data.SqlClient.SqlException e)
        //        {
        //            throw e;
        //        }
        //    }

        //}

        ///// <summary>
        ///// 执行Sql和Oracle滴混合事务
        ///// </summary>
        ///// <param name="list">SQL命令行列表</param>
        ///// <param name="oracleCmdSqlList">Oracle命令行列表</param>
        ///// <returns>执行结果 0-由于SQL造成事务失败 -1 由于Oracle造成事务失败 1-整体事务执行成功</returns>
        //public int ExecuteSqlTran(List<CommandInfo> list, List<CommandInfo> cmdSqlList)
        //{
        //    SqlConnection conn = IsInTransaction && _dbConnection != null
        //        ? _dbConnection as SqlConnection
        //        : new SqlConnection(ConnectionString);
        //    if (conn.State != ConnectionState.Open) conn.Open();
        //    SqlCommand cmd = new SqlCommand();
        //    cmd.Connection = conn;
        //    SqlTransaction tx = IsInTransaction ? this._dbTransaction as SqlTransaction : conn.BeginTransaction();
        //    cmd.Transaction = tx;
        //    try
        //    {
        //        foreach (CommandInfo myDE in list)
        //        {
        //            string cmdText = myDE.CommandText;
        //            SqlParameter[] cmdParms = (SqlParameter[])myDE.Parameters;
        //            PrepareCommand(cmd, conn, tx, cmdText, cmdParms);
        //            if (myDE.EffentNextType == EffentNextType.SolicitationEvent)
        //            {
        //                if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
        //                {
        //                    tx.Rollback();
        //                    throw new Exception("违背要求" + myDE.CommandText + "必须符合select count(..的格式");
        //                    //return 0;
        //                }

        //                object obj = cmd.ExecuteScalar();
        //                bool isHave = false;
        //                if (obj == null && obj == DBNull.Value)
        //                {
        //                    isHave = false;
        //                }
        //                isHave = Convert.ToInt32(obj) > 0;
        //                if (isHave)
        //                {
        //                    //引发事件
        //                    myDE.OnSolicitationEvent();
        //                }
        //            }
        //            if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine)
        //            {
        //                if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
        //                {
        //                    tx.Rollback();
        //                    throw new Exception("SQL:违背要求" + myDE.CommandText + "必须符合select count(..的格式");
        //                    //return 0;
        //                }

        //                object obj = cmd.ExecuteScalar();
        //                bool isHave = false;
        //                if (obj == null && obj == DBNull.Value)
        //                {
        //                    isHave = false;
        //                }
        //                isHave = Convert.ToInt32(obj) > 0;

        //                if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave)
        //                {
        //                    tx.Rollback();
        //                    throw new Exception("SQL:违背要求" + myDE.CommandText + "返回值必须大于0");
        //                    //return 0;
        //                }
        //                if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave)
        //                {
        //                    tx.Rollback();
        //                    throw new Exception("SQL:违背要求" + myDE.CommandText + "返回值必须等于0");
        //                    //return 0;
        //                }
        //                continue;
        //            }
        //            int val = cmd.ExecuteNonQuery();
        //            if (myDE.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0)
        //            {
        //                tx.Rollback();
        //                throw new Exception("SQL:违背要求" + myDE.CommandText + "必须有影响行");
        //                //return 0;
        //            }
        //            cmd.Parameters.Clear();
        //        }
        //        string oraConnectionString = PubConstant.GetConnectionString("ConnectionStringPPC");
        //        bool res = OracleHelper.ExecuteSqlTran(oraConnectionString, cmdSqlList);
        //        if (!res)
        //        {
        //            tx.Rollback();
        //            throw new Exception("Oracle执行失败");
        //            // return -1;
        //        }
        //        if (!IsInTransaction) tx.Commit();
        //        return 1;
        //    }
        //    catch (System.Data.SqlClient.SqlException e)
        //    {
        //        tx.Rollback();
        //        throw e;
        //    }
        //    catch (Exception e)
        //    {
        //        tx.Rollback();
        //        throw e;
        //    }
        //}


        /// <summary>
        /// 执行多条SQL语句，实现数据库事务。
        /// </summary>
        /// <param name="SQLStringList">多条SQL语句</param>		
        public int ExecuteSqlTran(List<String> SQLStringList)
        {
            SqlConnection conn = IsInTransaction && _dbConnection != null
                ? _dbConnection as SqlConnection
                : new SqlConnection(ConnectionString);
            if (conn.State != ConnectionState.Open) conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            SqlTransaction tx;
            if (!IsInTransaction)
            {
                tx = conn.BeginTransaction();
            }
            else
            {
                tx = this._dbTransaction as SqlTransaction;
            }
            cmd.Transaction = tx;
            try
            {
                int count = 0;
                for (int n = 0; n < SQLStringList.Count; n++)
                {
                    string strsql = SQLStringList[n];
                    if (strsql.Trim().Length > 1)
                    {
                        cmd.CommandText = strsql;
                        count += cmd.ExecuteNonQuery();
                    }
                }
                if (!IsInTransaction) tx.Commit();
                return count;
            }
            catch (Exception e)
            {
                tx.Rollback();
                throw e;
            }

        }
        /// <summary>
        /// 执行带一个存储过程参数的的SQL语句。
        /// </summary>
        /// <param name="sqlString">SQL语句</param>
        /// <param name="content">参数内容,比如一个字段是格式复杂的文章，有特殊符号，可以通过这个方式添加</param>
        /// <returns>影响的记录数</returns>
        public int ExecuteSql(string sqlString, string content)
        {
            SqlConnection conn = IsInTransaction && _dbConnection != null
                ? _dbConnection as SqlConnection
                : new SqlConnection(ConnectionString);


            //SqlTransaction tx = IsInTransaction ? this._dbTransaction as SqlTransaction : conn.BeginTransaction();
            SqlCommand cmd = new SqlCommand(sqlString, conn);

            SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText);
            myParameter.Value = content;
            cmd.Parameters.Add(myParameter);
            try
            {
                if (conn.State != ConnectionState.Open) conn.Open();
                SqlTransaction tx = IsInTransaction ? this._dbTransaction as SqlTransaction : conn.BeginTransaction();
                cmd.Transaction = tx;
                int rows = cmd.ExecuteNonQuery();
                if (!IsInTransaction) tx.Commit();
                return rows;
            }
            catch (SqlException e)
            {
                throw e;
            }
            finally
            {
                cmd.Dispose();
                if (IsInTransaction == false) conn.Close();
            }

        }
        /// <summary>
        /// 执行带一个存储过程参数的的SQL语句。
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <param name="content">参数内容,比如一个字段是格式复杂的文章，有特殊符号，可以通过这个方式添加</param>
        /// <returns>影响的记录数</returns>
        //public object ExecuteSqlGet(string SQLString, string content)
        //{
        //    SqlConnection conn = IsInTransaction && _dbConnection != null
        //       ? _dbConnection as SqlConnection
        //       : new SqlConnection(ConnectionString);

        //    //SqlCommand cmd = new SqlCommand(SQLString, conn);
        //    using (SqlCommand cmd = new SqlCommand(SQLString, conn))
        //    {
        //        SqlParameter myParameter = new SqlParameter("@content", SqlDbType.NText);
        //        myParameter.Value = content;
        //        cmd.Parameters.Add(myParameter);
        //        try
        //        {
        //            if (conn.State != ConnectionState.Open) conn.Open();
        //            SqlTransaction tx = IsInTransaction
        //                ? this._dbTransaction as SqlTransaction
        //                : conn.BeginTransaction();
        //            cmd.Transaction = tx;
        //            object obj = cmd.ExecuteScalar();
        //            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
        //            {
        //                return null;
        //            }
        //            else
        //            {
        //                return obj;
        //            }
        //        }
        //        catch (SqlException e)
        //        {
        //            throw e;
        //        }
        //        finally
        //        {
        //            cmd.Dispose();
        //            if (IsInTransaction == false) conn.Close();
        //        }
        //    }

        //}

        /// <summary>
        /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
        /// </summary>
        /// <param name="strSQL">SQL语句</param>
        /// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
        /// <returns>影响的记录数</returns>
        public int ExecuteSqlInsertImg(string strSQL, byte[] fs)
        {
            SqlConnection conn = IsInTransaction && _dbConnection != null
                ? _dbConnection as SqlConnection
                : new SqlConnection(ConnectionString);

            using (SqlCommand cmd = new SqlCommand(strSQL, conn))
            {
                System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@fs",
                    SqlDbType.Image);
                myParameter.Value = fs;
                cmd.Parameters.Add(myParameter);
                try
                {
                    if (conn.State != ConnectionState.Open) conn.Open();
                    SqlTransaction tx = IsInTransaction ? this._dbTransaction as SqlTransaction : conn.BeginTransaction();
                    cmd.Transaction = tx;
                    int rows = cmd.ExecuteNonQuery();
                    if (!IsInTransaction) tx.Commit();
                    return rows;
                }
                catch (System.Data.SqlClient.SqlException e)
                {
                    throw e;
                }
                finally
                {
                    cmd.Dispose();
                    if (IsInTransaction == false) conn.Close();
                }
            }
        }

        /// <summary>
        /// 执行一条计算查询结果语句，返回查询结果（object）。
        /// </summary>
        /// <param name="SQLString">计算查询结果语句</param>
        /// <returns>查询结果（object）</returns>
        public object GetSingle(string SQLString)
        {
            SqlConnection conn = IsInTransaction && _dbConnection != null
              ? _dbConnection as SqlConnection
              : new SqlConnection(ConnectionString);

            using (SqlCommand cmd = new SqlCommand(SQLString, conn))
            {
                try
                {
                    if (conn.State != ConnectionState.Open) conn.Open();
                    if (IsInTransaction == true) cmd.Transaction = (this._dbTransaction as SqlTransaction);
                    object obj = cmd.ExecuteScalar();
                    if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                    {
                        return null;
                    }
                    else
                    {
                        return obj;
                    }
                }
                catch (SqlException e)
                {
                    throw e;
                }
                finally
                {
                    if (IsInTransaction == false) conn.Close();
                }
            }
        }
        //public object GetSingle(string SQLString, int Times)
        //{
        //    SqlConnection conn = IsInTransaction && _dbConnection != null
        //         ? _dbConnection as SqlConnection
        //         : new SqlConnection(ConnectionString);
        //    using (SqlCommand cmd = new SqlCommand(SQLString, conn))
        //    {
        //        try
        //        {
        //            if (conn.State != ConnectionState.Open) conn.Open();
        //            cmd.CommandTimeout = Times;
        //            if (IsInTransaction == true) cmd.Transaction = (this._dbTransaction as SqlTransaction);
        //            object obj = cmd.ExecuteScalar();
        //            if ((Equals(obj, null)) || (Equals(obj, System.DBNull.Value)))
        //            {
        //                return null;
        //            }
        //            return obj;
        //        }
        //        catch (SqlException e)
        //        {
        //            throw e;
        //        }
        //        finally
        //        {
        //            if (IsInTransaction == false) conn.Close();
        //        }
        //    }

        //}
        /// <summary>
        /// 执行查询语句，返回SqlDataReader ( 注意：调用该方法后，一定要对SqlDataReader进行Close )
        /// </summary>
        /// <param name="strSQL">查询语句</param>
        /// <returns>SqlDataReader</returns>
        public SqlDataReader ExecuteReader(string strSQL)
        {
            SqlConnection conn = IsInTransaction && _dbConnection != null
                ? _dbConnection as SqlConnection
                : new SqlConnection(ConnectionString);
            using (SqlCommand cmd = new SqlCommand(strSQL, conn))
            {
                try
                {
                    if (conn.State != ConnectionState.Open) conn.Open();
                    if (IsInTransaction)
                    {
                        cmd.Transaction = this._dbTransaction as SqlTransaction;
                    }
                    SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    return myReader;
                }
                catch (SqlException e)
                {
                    LogHelper.WriteError("ExecuteReader", e);
                    throw e;
                }
            }

        }
        /// <summary>
        /// 执行查询语句，返回DataSet
        /// </summary>
        /// <param name="sqlString">查询语句</param>
        /// <returns>DataSet</returns>
        public DataSet Query(string sqlString)
        {
            SqlConnection conn = !IsInTransaction
                  ? new SqlConnection(ConnectionString)
                  : this._dbConnection as SqlConnection;
            DataSet ds = new DataSet();
            try
            {
                if (conn.State != ConnectionState.Open) conn.Open();
                SqlDataAdapter command = new SqlDataAdapter(sqlString, conn);
                if (IsInTransaction)
                {
                    command.SelectCommand.Transaction = this._dbTransaction as SqlTransaction;
                }
                command.Fill(ds, "ds");
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                conn.Close();
                throw new Exception(ex.Message);
            }

            if (!IsInTransaction)
            {
                conn.Close();
            }

            return ds;
        }


        //public DataSet Query(string sqlString, int Times)
        //{
        //    SqlConnection conn = !IsInTransaction
        //          ? new SqlConnection(ConnectionString)
        //          : this._dbConnection as SqlConnection;
        //    DataSet ds = new DataSet();
        //    try
        //    {
        //        if (conn.State != ConnectionState.Open) conn.Open();
        //        SqlDataAdapter command = new SqlDataAdapter(sqlString, conn);
        //        command.SelectCommand.CommandTimeout = Times;
        //        if (IsInTransaction)
        //        {
        //            command.SelectCommand.Transaction = this._dbTransaction as SqlTransaction;
        //        }
        //        command.Fill(ds, "ds");
        //    }
        //    catch (System.Data.SqlClient.SqlException ex)
        //    {
        //        throw new Exception(ex.Message);
        //    }
        //    finally
        //    {
        //        if (!IsInTransaction)
        //        {
        //            conn.Close();
        //        }
        //    }

        //    return ds;

        //}



        #endregion

        #region 执行带参数的SQL语句

        /// <summary>
        /// 执行SQL语句，返回影响的记录数
        /// </summary>
        /// <param name="sqlString">SQL语句</param>
        /// <param name="cmdParms"></param>
        /// <returns>影响的记录数</returns>
        public int ExecuteSql(string sqlString, params SqlParameter[] cmdParms)
        {
            SqlConnection conn = !IsInTransaction
                 ? new SqlConnection(ConnectionString)
                 : this._dbConnection as SqlConnection;

            using (SqlCommand cmd = new SqlCommand())
            {
                try
                {
                    if (conn.State != ConnectionState.Open) conn.Open();
                    SqlTransaction tx = IsInTransaction ? this._dbTransaction as SqlTransaction : conn.BeginTransaction();
                    PrepareCommand(cmd, conn, tx, sqlString, cmdParms);
                    int rows = cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                    if (!IsInTransaction) tx.Commit();
                    return rows;
                }
                catch (SqlException e)
                {
                    LogHelper.WriteError("ExecuteSql", e);
                    throw e;
                }
            }

        }

        /// <summary>
        /// 执行多条SQL语句，实现数据库事务。
        /// </summary>
        /// <param name="SQLStringList">SQL语句的哈希表（key为sql语句，value是该语句的SqlParameter[]）</param>
        public void ExecuteSqlTran(Hashtable SQLStringList)
        {
            SqlConnection conn = !IsInTransaction
                 ? new SqlConnection(ConnectionString)
                 : this._dbConnection as SqlConnection;

            if (conn.State != ConnectionState.Open) conn.Open();
            SqlTransaction trans = !IsInTransaction ? conn.BeginTransaction() : this._dbTransaction as SqlTransaction;
            SqlCommand cmd = new SqlCommand();
            try
            {
                //循环
                foreach (DictionaryEntry myDE in SQLStringList)
                {
                    string cmdText = myDE.Key.ToString();
                    SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;
                    PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
                    int val = cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                }
                if (!IsInTransaction) trans.Commit();
            }
            catch
            {
                trans.Rollback();
                throw;
            }

        }

        ///// <summary>
        ///// 执行多条SQL语句，实现数据库事务。
        ///// </summary>
        //public int ExecuteSqlTran(List<CommandInfo> cmdList)
        //{
        //    if (cmdList == null) throw new ArgumentNullException("cmdList");

        //    SqlConnection conn = IsInTransaction && _dbConnection != null
        //        ? _dbConnection as SqlConnection
        //        : new SqlConnection(ConnectionString);


        //    if (conn.State != ConnectionState.Open) conn.Open();
        //    SqlTransaction trans = !IsInTransaction ? conn.BeginTransaction() : this._dbTransaction as SqlTransaction;
        //    SqlCommand cmd = new SqlCommand();
        //    try
        //    {
        //        int count = 0;
        //        //循环
        //        foreach (CommandInfo myDE in cmdList)
        //        {
        //            string cmdText = myDE.CommandText;
        //            SqlParameter[] cmdParms = (SqlParameter[])myDE.Parameters;
        //            PrepareCommand(cmd, conn, trans, cmdText, cmdParms);

        //            if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine)
        //            {
        //                if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
        //                {
        //                    if (trans != null) trans.Rollback();
        //                    return 0;
        //                }

        //                object obj = cmd.ExecuteScalar();
        //                bool isHave = false;
        //                if (obj == null && obj == DBNull.Value)
        //                {
        //                    isHave = false;
        //                }
        //                isHave = Convert.ToInt32(obj) > 0;

        //                if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave)
        //                {
        //                    if (trans != null) trans.Rollback();
        //                    return 0;
        //                }
        //                if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave)
        //                {
        //                    if (trans != null) trans.Rollback();
        //                    return 0;
        //                }
        //                continue;
        //            }
        //            int val = cmd.ExecuteNonQuery();
        //            count += val;
        //            if (myDE.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0)
        //            {
        //                if (trans != null) trans.Rollback();
        //                return 0;
        //            }
        //            cmd.Parameters.Clear();
        //        }
        //        if (!IsInTransaction) if (trans != null) trans.Commit();
        //        return count;
        //    }
        //    catch
        //    {
        //        trans.Rollback();
        //        throw;
        //    }
        //}
        ///// <summary>
        ///// 执行多条SQL语句，实现数据库事务。
        ///// </summary>
        ///// <param name="sqlStringList">SQL语句的哈希表（key为sql语句，value是该语句的SqlParameter[]）</param>
        //public void ExecuteSqlTranWithIndentity(List<CommandInfo> sqlStringList)
        //{
        //    SqlConnection conn = IsInTransaction && _dbConnection != null
        //        ? _dbConnection as SqlConnection
        //        : new SqlConnection(ConnectionString);
        //    if (conn.State != ConnectionState.Open) conn.Open();
        //    SqlTransaction trans = !IsInTransaction ? conn.BeginTransaction() : this._dbTransaction as SqlTransaction;

        //    SqlCommand cmd = new SqlCommand();
        //    try
        //    {
        //        int indentity = 0;
        //        //循环
        //        foreach (CommandInfo myDe in sqlStringList)
        //        {
        //            string cmdText = myDe.CommandText;
        //            SqlParameter[] cmdParms = (SqlParameter[])myDe.Parameters;
        //            foreach (SqlParameter q in cmdParms)
        //            {
        //                if (q.Direction == ParameterDirection.InputOutput)
        //                {
        //                    q.Value = indentity;
        //                }
        //            }
        //            PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
        //            cmd.ExecuteNonQuery();
        //            foreach (SqlParameter q in cmdParms)
        //            {
        //                if (q.Direction == ParameterDirection.Output)
        //                {
        //                    indentity = Convert.ToInt32(q.Value);
        //                }
        //            }
        //            cmd.Parameters.Clear();
        //        }
        //        if (!IsInTransaction) if (trans != null) trans.Commit();
        //    }
        //    catch
        //    {
        //        trans.Rollback();
        //        throw;
        //    }

        //}
        ///// <summary>
        ///// 执行多条SQL语句，实现数据库事务。
        ///// </summary>
        ///// <param name="sqlStringList">SQL语句的哈希表（key为sql语句，value是该语句的SqlParameter[]）</param>
        //public void ExecuteSqlTranWithIndentity(Hashtable sqlStringList)
        //{
        //    SqlConnection conn = IsInTransaction && _dbConnection != null
        //         ? _dbConnection as SqlConnection
        //         : new SqlConnection(ConnectionString);

        //    if (conn.State != ConnectionState.Open) conn.Open();
        //    SqlTransaction trans = !IsInTransaction ? conn.BeginTransaction() : this._dbTransaction as SqlTransaction;

        //    SqlCommand cmd = new SqlCommand();
        //    try
        //    {
        //        int indentity = 0;
        //        //循环
        //        foreach (DictionaryEntry myDE in sqlStringList)
        //        {
        //            string cmdText = myDE.Key.ToString();
        //            SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;
        //            foreach (SqlParameter q in cmdParms)
        //            {
        //                if (q.Direction == ParameterDirection.InputOutput)
        //                {
        //                    q.Value = indentity;
        //                }
        //            }
        //            PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
        //            cmd.ExecuteNonQuery();
        //            foreach (SqlParameter q in cmdParms)
        //            {
        //                if (q.Direction == ParameterDirection.Output)
        //                {
        //                    indentity = Convert.ToInt32(q.Value);
        //                }
        //            }
        //            cmd.Parameters.Clear();
        //        }
        //        if (!IsInTransaction) if (trans != null) trans.Commit();
        //    }
        //    catch
        //    {
        //        if (trans != null) trans.Rollback();
        //        throw;
        //    }


        //}
        ///// <summary>
        ///// 执行一条计算查询结果语句，返回查询结果（object）。
        ///// </summary>
        ///// <param name="SQLString">计算查询结果语句</param>
        ///// <returns>查询结果（object）</returns>
        public object GetSingle(string SQLString, params SqlParameter[] cmdParms)
        {
            SqlConnection conn = IsInTransaction && _dbConnection != null
                ? _dbConnection as SqlConnection
                : new SqlConnection(ConnectionString);

            using (SqlCommand cmd = new SqlCommand())
            {
                SqlTransaction trans = null;
                try
                {
                    if (conn.State == ConnectionState.Broken || conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    trans = !IsInTransaction
                        ? conn.BeginTransaction()
                        : this._dbTransaction as SqlTransaction;
                    PrepareCommand(cmd, conn, trans, SQLString, cmdParms);
                    object obj = cmd.ExecuteScalar();
                    cmd.Parameters.Clear();
                    if (!IsInTransaction) trans.Commit();
                    if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                    {
                        return null;
                    }
                    return obj;

                }
                catch (SqlException e)
                {
                    trans.Rollback();
                    throw e;
                }
                finally
                {
                    if (IsInTransaction == false) conn.Close();
                }
            }


        }

        /// <summary>
        /// 执行查询语句，返回SqlDataReader ( 注意：调用该方法后，一定要对SqlDataReader进行Close )
        /// </summary>
        /// <param name="sqlString"></param>
        /// <param name="cmdParms"></param>
        /// <returns>SqlDataReader</returns>
        public SqlDataReader ExecuteReader(string sqlString, params SqlParameter[] cmdParms)
        {
            SqlConnection conn = IsInTransaction && _dbConnection != null
                ? _dbConnection as SqlConnection
                : new SqlConnection(ConnectionString);

            if (conn.State != ConnectionState.Open) conn.Open();
            SqlTransaction tx = IsInTransaction ? this._dbTransaction as SqlTransaction : conn.BeginTransaction();
            SqlCommand cmd = new SqlCommand();
            try
            {
                PrepareCommand(cmd, conn, tx, sqlString, cmdParms);
                SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                cmd.Parameters.Clear();
                return myReader;
            }
            catch (SqlException e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 执行查询语句，返回DataSet
        /// </summary>
        /// <param name="sqlString">查询语句</param>
        /// <param name="cmdParms"></param>
        /// <returns>DataSet</returns>
        public DataSet Query(string sqlString, params SqlParameter[] cmdParms)
        {
            SqlConnection conn = !IsInTransaction
                  ? new SqlConnection(ConnectionString)
                  : this._dbConnection as SqlConnection;
            SqlCommand cmd = new SqlCommand();
            SqlTransaction tx = IsInTransaction ? this._dbTransaction as SqlTransaction : null;
            PrepareCommand(cmd, conn, tx, sqlString, cmdParms);
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                DataSet ds = new DataSet();
                try
                {
                    da.Fill(ds, "ds");
                    cmd.Parameters.Clear();
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    if (!IsInTransaction)
                    {
                        conn.Close();
                    }
                }
                return ds;
            }
        }


        private void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, IEnumerable<SqlParameter> cmdParms)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            if (trans != null)
                cmd.Transaction = trans;

            cmd.CommandType = CommandType.Text;//cmdType;
            if (cmdParms != null)
            {


                foreach (SqlParameter parameter in cmdParms)
                {
                    if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
                        (parameter.Value == null))
                    {
                        parameter.Value = DBNull.Value;
                    }
                    cmd.Parameters.Add(parameter);
                }
            }
        }

        #endregion

        #region 存储过程操作

        /// <summary>
        /// 执行存储过程，返回SqlDataReader ( 注意：调用该方法后，一定要对SqlDataReader进行Close )
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <returns>SqlDataReader</returns>
        public SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
        {
            SqlConnection connection = new SqlConnection(ConnectionString);
            if (connection.State != ConnectionState.Open) connection.Open();
            SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
            command.CommandType = CommandType.StoredProcedure;
            SqlDataReader returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
            return returnReader;

        }


        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <param name="tableName">DataSet结果中的表名</param>
        /// <returns>DataSet</returns>
        public DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                DataSet dataSet = new DataSet();
                if (connection.State != ConnectionState.Open) connection.Open();
                SqlDataAdapter sqlDa = new SqlDataAdapter
                {
                    SelectCommand = BuildQueryCommand(connection, storedProcName, parameters)
                };
                sqlDa.Fill(dataSet, tableName);
                connection.Close();
                return dataSet;
            }
        }
        //public DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName, int Times)
        //{
        //    using (SqlConnection connection = new SqlConnection(ConnectionString))
        //    {
        //        DataSet dataSet = new DataSet();
        //        if (connection.State != ConnectionState.Open) connection.Open();
        //        using (SqlDataAdapter sqlDA = new SqlDataAdapter())
        //        {
        //            sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
        //            sqlDA.SelectCommand.CommandTimeout = Times;
        //            sqlDA.Fill(dataSet, tableName);
        //        }
        //        connection.Close();
        //        return dataSet;
        //    }
        //}


        /// <summary>
        /// 构建 SqlCommand 对象(用来返回一个结果集，而不是一个整数值)
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <returns>SqlCommand</returns>
        private SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IEnumerable<IDataParameter> parameters)
        {
            SqlCommand command = new SqlCommand(storedProcName, connection) { CommandType = CommandType.StoredProcedure };
            foreach (SqlParameter parameter in parameters)
            {
                if (parameter != null)
                {
                    // 检查未分配值的输出参数,将其分配以DBNull.Value.
                    if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
                        (parameter.Value == null))
                    {
                        parameter.Value = DBNull.Value;
                    }
                    command.Parameters.Add(parameter);
                }
            }

            return command;
        }

        /// <summary>
        /// 执行存储过程，返回影响的行数		
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <param name="rowsAffected">影响的行数</param>
        /// <returns></returns>
        public int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                if (connection.State != ConnectionState.Open) connection.Open();
                SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
                rowsAffected = command.ExecuteNonQuery();
                int result = (int)command.Parameters["ReturnValue"].Value;
                //Connection.Close();
                return result;
            }
        }

        /// <summary>
        /// 创建 SqlCommand 对象实例(用来返回一个整数值)	
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <returns>SqlCommand 对象实例</returns>
        private SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IEnumerable<IDataParameter> parameters)
        {
            SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
            command.Parameters.Add(new SqlParameter("ReturnValue",
                SqlDbType.Int, 4, ParameterDirection.ReturnValue,
                false, 0, 0, string.Empty, DataRowVersion.Default, null));
            return command;
        }
        #endregion

        public void Dispose()
        {
            SqlConnection conn = _dbConnection as SqlConnection;
            if (conn != null) conn.Close();
            _dbConnection = null;
            _dbTransaction = null;
        }
    }

}
